<?php

App::uses('FileSystem', 'Base.Lib');

class TestEnvironment {

    public function __construct($root, $clear = false) {
        $this->root = $root;

        if (!file_exists($this->root)) {
            throw new Exception("{$this->root} não existe");
        }

        if (!is_dir($this->root)) {
            throw new Exception("{$this->root} não é um diretório");
        }

        if ($clear) {
            if (!FileSystem::recursiveRemoveDirectoryInners($this->root)) {
                throw new Exception("Não foi possível remover todo o conteúdo de \"{$this->root}\"");
            }
        }
        
        $oldMask = umask();
        umask(0);

        try {

            if (!mkdir($this->getRawFilesDirectory())) {
                throw new Exception("{$this->getRawFilesDirectory()} não pode ser criado");
            }
            if (!mkdir($this->getSharedFilesDirectory())) {
                throw new Exception("{$this->getRawFilesDirectory()} não pode ser criado");
            }

            $this->_createRawFiles();
            $this->_createTags();
            $this->_createRawFileTags();
        } catch (Exception $ex) {
            umask($oldMask);
            throw $ex;
        }
        
        umask($oldMask);
    }

    public function getRoot() {
        return $this->root;
    }

    public function getRawFilesDirectory() {
        return $this->root . '/raw_files_directory';
    }

    public function getSharedFilesDirectory() {
        return $this->root . '/shared_files_directory';
    }

    private function _createRawFiles() {
        $RawFile = ClassRegistry::init('RawFile');
        foreach ($RawFile->find('all') as $rawFile) {
            file_put_contents($this->getRawFilesDirectory() . DS . $rawFile['RawFile']['name'], '');
        }
    }

    private function _createTags() {
        $Tag = ClassRegistry::init('Tag');
        foreach ($Tag->find('all') as $tag) {
            mkdir($this->getSharedFilesDirectory() . DS . $tag[$Tag->alias]['name']);
        }
    }

    private function _createRawFileTags() {
        $Tag = ClassRegistry::init('Tag');
        $RawFile = ClassRegistry::init('RawFile');
        $RawFileTag = ClassRegistry::init('RawFileTag');
        foreach ($RawFileTag->find('all') as $rawFileTag) {
            $tag = $Tag->findById($rawFileTag['RawFileTag']['tag_id']);
            $rawFile = $RawFile->findById($rawFileTag['RawFileTag']['raw_file_id']);
            symlink(
                    $this->getRawFilesDirectory() . DS . $rawFile['RawFile']['name']
                    , $this->getSharedFilesDirectory() . DS . $tag['Tag']['name'] . DS . $rawFile['RawFile']['name']
            );
        }
    }

    public function apply() {
        ConfigurationKeys::setKeyValue(
                'raw_files_directory'
                , $this->getRawFilesDirectory()
        );
        ConfigurationKeys::setKeyValue(
                'shared_files_directory'
                , $this->getSharedFilesDirectory()
        );
    }

}
